﻿/*--------------------------------------
 * Author: 刘楠   Date: 08/10/2007
 * -------------------------------------*/

using System;
using System.Data;
using System.Text;
using Entity;
using BussinessLib;
using UserInfo;
/// <summary>
/// OrderManage 的摘要说明
/// </summary>
public class OrderManage
{
	public OrderManage()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    /// <summary>
    /// 插入一条订餐记录
    /// </summary>
    /// <param name="record">订单</param>
    /// <returns></returns>
    public string AddRecord(Order record)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("insert into OrderRecord (ord_dtBookTime,ord_dtArrivalTime,ord_nTableNum,ord_nState,ord_sDesc,ord_nUserID) values(");
        sb.Append("'"+ record.BookTime + "','" + record.ArrivalTime + "'," + record.TableNum + "," + record.TableID+",'"+record.Description+"',"+record.UserID+");");//tableID为餐馆号

        Helper hp = new Helper();

        hp.ExecuteSql(sb.ToString());    //执行插入

        Best bs = new Best(record);
        return bs.GetBest();
        //----------------------------求最大折扣
       
        //float mDiscount = GetDiscountByLevel(GetUserLevel(record.UserID
        //    ));

        ////按订单前N名打折折扣
        //float orderDiscount;

        //int num = GetOrderNum(record.ArrivalTime, record.TableID);

        //if (num == 1)
        //    orderDiscount = (float)7;
        //else if (num == 2)
        //    orderDiscount = (float)8.5;
        //else if (num == 3)
        //    orderDiscount = (float)8.8;
        //else
        //    orderDiscount = (float)10;
        ////淡旺季打折
        //float timeDiscount;
        //if ((record.ArrivalTime.Month % 2) == 0) //双月为淡季
        //    timeDiscount = (float)8.5;
        //else
        //    timeDiscount = (float)10;
        //float result;
        //StringBuilder context = new StringBuilder();

        ////求最大职位 
        //if (mDiscount > timeDiscount)
        //{
        //    if (timeDiscount > orderDiscount)
        //    {
        //        result = orderDiscount;
        //        context.Append("您是第" + num + "名下订单的，可以享受" + result + "折优惠！");
        //    }
        //    else
        //    {
        //        result = timeDiscount;
        //        context.Append("您是在淡季下订单的，可以享受餐厅" + result + "折优惠！");
        //    }
        //}
        //else
        //{
        //    if (mDiscount > orderDiscount)
        //    {
        //        result = orderDiscount;
        //        context.Append("您是第" + num + "名下订单的，可以享受" + result + "折优惠！");
        //    }
        //    else
        //    {
        //        result = mDiscount;
        //        context.Append("您是" + GetUserLevel(record.UserID
        //    ) + "级会员，可以享受" + result + "折优惠！");
        //    }
        //}

        //return context.ToString();

    }

    public bool DeleteRecord(int id)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("DELETE FROM OrderRecord WHERE (ord_nID =" + id + ");");

        Helper hp = new Helper();

        if( hp.ExecuteSql(sb.ToString()))
            return true;
        else
            return false;
    }

    private float GetDiscountByLevel(int level)
    {
        StringBuilder sb = new StringBuilder("select * from cheap where C_nLevel = " + level);
        Helper hp = new Helper();
        DataTable t = hp.ExecuteDataTable(sb.ToString());
         float dis ;
         if (t.Rows.Count != 0)
             dis = (float)t.Rows[0][1];
         else
             dis = (float)10;

         return dis;

    }

    private int GetUserLevel(int uid)
    {
        StringBuilder sb = new StringBuilder("select * from User where U_nID= " + uid);
        Helper hp = new Helper();
        DataTable t = hp.ExecuteDataTable(sb.ToString());
        int Level =1;
        if (t != null)
        {
            if (t.Rows.Count != 0)
                Level = (int)t.Rows[0]["U_nLevel"];
        }
        else
            Level = 1;

        return Level;
    }

    public int GetOrderNum(DateTime day, int resID)
    {
        StringBuilder sb = new StringBuilder("select * from orderRecord where  datediff('d','" + day.ToString() + "',ord_dtArrivalTime)=0 and ord_nState=" + resID);
        Helper hp = new Helper();

        DataTable t = hp.ExecuteDataTable(sb.ToString());

        return t.Rows.Count;
    }
    
}
